﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
namespace PriceCuttingWar
{
    public partial class MainPage : PhoneApplicationPage
    {
        public List<ItemInfo> FoodData = new List<ItemInfo>();
        public MainPage()
        {
            InitializeComponent();
            ANI_Zhengque.Completed += new EventHandler(ANI_Zhengque_Completed);
            ANI_Cuowu.Completed += new EventHandler(ANI_Cuowu_Completed);
            GSystem.Root = this;
            GameScore = 0;
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);

            Card1.MouseLeftButtonDown += new MouseButtonEventHandler(Card_MouseLeftButtonDown);
            Card2.MouseLeftButtonDown += new MouseButtonEventHandler(Card_MouseLeftButtonDown);
            Card3.MouseLeftButtonDown += new MouseButtonEventHandler(Card_MouseLeftButtonDown);
            Card4.MouseLeftButtonDown += new MouseButtonEventHandler(Card_MouseLeftButtonDown);
            Card5.MouseLeftButtonDown += new MouseButtonEventHandler(Card_MouseLeftButtonDown);
            Card6.MouseLeftButtonDown += new MouseButtonEventHandler(Card_MouseLeftButtonDown);
        }
        public static bool isStartShow { get; set; }
        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            if (!isStartShow)
            {
                e.Cancel = true;
                StartInterfaceShow();
            }
            else
                e.Cancel = false;
            base.OnBackKeyPress(e);
        }
        void EnabledCards()
        {
            Card1.IsEnabled = true;
            Card2.IsEnabled = true;
            Card3.IsEnabled = true;
            Card4.IsEnabled = true;
            Card5.IsEnabled = true;
            Card6.IsEnabled = true;
        }
        void UnEnabledCards()
        {
            Card1.IsEnabled = false;
            Card2.IsEnabled = false;
            Card3.IsEnabled = false;
            Card4.IsEnabled = false;
            Card5.IsEnabled = false;
            Card6.IsEnabled = false;
        }
        void Card_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            AssertionSelected((sender as UICard).FoodInfo.Type);
        }
        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            StartInterfaceShow();
        }
        public void StartInterfaceShow()
        {
            isStartShow = true;
            StartGameInterface.ANI_Show.Begin();
            StartGameInterface.Rest();
            GameMain.Visibility = Visibility.Collapsed;
        }
        public void StartGame()
        {
            HideAllItem();
            //GSystem.UISelected.IsEnabled = true;
            EnabledCards();
            GameScore = 0;
            GameMain.Visibility = Visibility.Visible;
            GameingLevel = 1;
            GSystem.UIProgress.MaxSecond = 60;
            GSystem.UIProgress.TimeStart();
        }
        void ANI_Cuowu_Completed(object sender, EventArgs e)
        {
            GameingLevel -= 1;
            //GSystem.UISelected.IsEnabled = true;
            EnabledCards();
        }

        void ANI_Zhengque_Completed(object sender, EventArgs e)
        {
            GameScore += GameingLevel * 5 * GameingLevel;
            GameingLevel += 1;
            //GSystem.UISelected.IsEnabled = true;
            EnabledCards();
        }
        public int ZhengqueType = 0;
        public void AssertionSelected(int type)
        {
            //GSystem.UISelected.IsEnabled = false;
            UnEnabledCards();
            
            if (ZhengqueType == type)
                ANI_Zhengque.Begin();
            else
                ANI_Cuowu.Begin();
        }

        private int _GameScore = 0;

        public int GameScore
        {
            get { return _GameScore; }
            set
            {
                _GameScore = value;
                ScoreSum.Text = value.ToString();
            }
        }
        public List<ItemInfo> GameingItem = new List<ItemInfo>();
        private void HideAllItem()
        {
            GameingItem.Clear();
            W1.Visibility = Visibility.Collapsed;
            W2.Visibility = Visibility.Collapsed;
            Card1.Visibility = Visibility.Collapsed;
            Card2.Visibility = Visibility.Collapsed;
            Card3.Visibility = Visibility.Collapsed;
            Card4.Visibility = Visibility.Collapsed;
            Card5.Visibility = Visibility.Collapsed;
            Card6.Visibility = Visibility.Collapsed;
        }
        private void CreateListItemInfo(int max)
        {
            for (int i = 0; i < max; i++)
            {
                ItemInfo s;
                bool r = false;
                do
                {
                    r = false;
                    s.Type = GSystem.GamesRandom.Next() % 6 + 1;
                    foreach (var item in GameingItem)
                    {
                        if (s.Type == item.Type)
                            r = true;


                    }
                } while (r);
                s.Money = GSystem.GamesRandom.Next(50, 100);
                s.Money2 = GSystem.GamesRandom.Next(45, (int)(s.Money * 0.95));
                GameingItem.Add(s);
            }
        }
        private void overCreate()
        {
            bool isRun = true;
            ItemInfo zuida = GameingItem[0];

            while (isRun)
            {
                isRun = false;
                for (int i = 0; i < GameingItem.Count; i++)
                {
                    if (zuida.Type == GameingItem[i].Type)
                    {
                        continue;
                    }
                    else
                    {
                        if ((zuida.Money - zuida.Money2) == (GameingItem[i].Money - GameingItem[i].Money2))
                        {
                            ItemInfo s = new ItemInfo();
                            s.Money = GSystem.GamesRandom.Next(50, 100);
                            s.Money2 = GSystem.GamesRandom.Next(45, (int)(s.Money * 0.95));
                            s.Type = GameingItem[i].Type;
                            GameingItem.RemoveAt(i);
                            GameingItem.Insert(i, s);
                            isRun = true;
                            break;
                        }
                        else
                        {
                            if ((zuida.Money - zuida.Money2) > (GameingItem[i].Money - GameingItem[i].Money2))
                            {
                                continue;
                            }
                            else
                            {
                                zuida = GameingItem[i];
                            }
                        }
                    }
                }
            }
            ZhengqueType = zuida.Type;
            for (int i = 0; i < GameingItem.Count; i++)
            {
                ItemInfo s = GameingItem[i];
                GameingItem.Remove(s);
                GameingItem.Insert(GSystem.GamesRandom.Next() % GameingItem.Count, s);
            }

            //GSystem.UISelected.UpDataCardList(GameingItem);
        }
        private int _GameingLevel = 1;
        public int GameingLevel
        {
            get { return _GameingLevel; }
            set
            {
                if (value <= 1)
                    _GameingLevel = 1;
                else
                    _GameingLevel = value;
                if (_GameingLevel >= 5)
                    _GameingLevel = 5;
                switch (_GameingLevel)
                {
                    case 1:
                        Level_1();
                        break;
                    case 2:
                        Level_2();
                        break;
                    case 3:
                        Level_3();
                        break;
                    case 4:
                        Level_4();
                        break;
                    case 5:
                        Level_5();
                        break;
                    default:
                        _GameingLevel = 1;
                        Level_1();
                        break;
                }
            }
        }
        public void Level_1()
        {
            HideAllItem();
            CreateListItemInfo(2);
            overCreate();
            W1.Visibility = Visibility.Visible;
            Card1.Visibility = Visibility.Visible;
            Card2.Visibility = Visibility.Visible;
            Card1.FoodInfo = GameingItem[0];
            Card2.FoodInfo = GameingItem[1];
        }
        public void Level_2()
        {
            HideAllItem();
            CreateListItemInfo(3);
            overCreate();
            W1.Visibility = Visibility.Visible;
            Card1.Visibility = Visibility.Visible;
            Card2.Visibility = Visibility.Visible;
            Card3.Visibility = Visibility.Visible;
            Card1.FoodInfo = GameingItem[0];
            Card2.FoodInfo = GameingItem[1];
            Card3.FoodInfo = GameingItem[2];
        }
        public void Level_3()
        {
            HideAllItem();
            CreateListItemInfo(4);
            overCreate();
            W1.Visibility = Visibility.Visible;
            W2.Visibility = Visibility.Visible;
            Card1.Visibility = Visibility.Visible;
            Card2.Visibility = Visibility.Visible;
            Card4.Visibility = Visibility.Visible;
            Card5.Visibility = Visibility.Visible;
            Card1.FoodInfo = GameingItem[0];
            Card2.FoodInfo = GameingItem[1];
            Card4.FoodInfo = GameingItem[2];
            Card5.FoodInfo = GameingItem[3];
        }
        public void Level_4()
        {
            HideAllItem();
            CreateListItemInfo(5);
            overCreate();
            W1.Visibility = Visibility.Visible;
            W2.Visibility = Visibility.Visible;
            Card1.Visibility = Visibility.Visible;
            Card2.Visibility = Visibility.Visible;
            Card3.Visibility = Visibility.Visible;
            Card4.Visibility = Visibility.Visible;
            Card5.Visibility = Visibility.Visible;
            Card1.FoodInfo = GameingItem[0];
            Card2.FoodInfo = GameingItem[1];
            Card3.FoodInfo = GameingItem[2];
            Card4.FoodInfo = GameingItem[3];
            Card5.FoodInfo = GameingItem[4];
        }
        public void Level_5()
        {
            HideAllItem();
            CreateListItemInfo(6);
            overCreate();
            W1.Visibility = Visibility.Visible;
            W2.Visibility = Visibility.Visible;
            Card1.Visibility = Visibility.Visible;
            Card2.Visibility = Visibility.Visible;
            Card3.Visibility = Visibility.Visible;
            Card4.Visibility = Visibility.Visible;
            Card5.Visibility = Visibility.Visible;
            Card6.Visibility = Visibility.Visible;
            Card1.FoodInfo = GameingItem[0];
            Card2.FoodInfo = GameingItem[1];
            Card3.FoodInfo = GameingItem[2];
            Card4.FoodInfo = GameingItem[3];
            Card5.FoodInfo = GameingItem[4];
            Card6.FoodInfo = GameingItem[5];
        }
    }
}
